package com.flc.gateway.config;

import com.flc.gateway.utils.PropertyFactory;

/**
 * @author haizi
 * @date 2018/1/2
 */
public class AppConfig {

    public NettyCfg getNettyCfg() {
        return nettyCfg;
    }

    public RocketMqCfg getRocketMqCfg() {
        return rocketMqCfg;
    }

    public RedisCfg getRedisCfg() {
        return redisCfg;
    }

    public ServerCfg getServerCfg() {
        return serverCfg;
    }

    public QueueCfg getQueueCfg() {
        return queueCfg;
    }

    private NettyCfg nettyCfg;
    private RocketMqCfg rocketMqCfg;
    private RedisCfg redisCfg;
    private ServerCfg serverCfg;
    private QueueCfg queueCfg;

    public class QueueCfg {

        public Integer getDiagnosisLength() {
            return diagnosisLength;
        }

        public Integer getLoginLength() {
            return loginLength;
        }

        public Integer getReportLength() {
            return reportLength;
        }

        public Integer getTrajectoryLength() {
            return trajectoryLength;
        }

        public Integer getCmdLength() {
            return cmdLength;
        }

        Integer diagnosisLength;
        Integer loginLength;
        Integer reportLength;
        Integer trajectoryLength;
        Integer cmdLength;

    }

    public class RocketMqCfg {

        public String getGroupName() {
            return groupName;
        }

        public String getNameServerAddr() {
            return nameServerAddr;
        }

        public String getDiagnosisTopic() {
            return diagnosisTopic;
        }

        public String getLoginTopic() {
            return loginTopic;
        }

        public String getReportTopic() {
            return reportTopic;
        }

        public String getCmdTopic() {
            return cmdTopic;
        }

        public String getTrajectoryTopic() {
            return trajectoryTopic;
        }

        String groupName;
        String nameServerAddr;
        String diagnosisTopic;
        String loginTopic;
        String reportTopic;
        String trajectoryTopic;
        String cmdTopic;

    }

    public class RedisCfg {

        public Integer getMaxIdle() {
            return maxIdle;
        }

        public Integer getMaxTotal() {
            return maxTotal;
        }

        public Integer getMaxWaitMillis() {
            return maxWaitMillis;
        }

        public String getHost() {
            return host;
        }

        public Integer getPort() {
            return port;
        }

        Integer maxIdle;
        Integer maxTotal;
        Integer maxWaitMillis;
        String  host;
        Integer port;
    }

    public class ServerCfg {

        public String getID() {
            return id;
        }

        public String getName() {
            return name;
        }

        String id;
        String name;
    }

    public class NettyCfg {

        public Integer getPort() {
            return port;
        }

        public Integer getBacklog() {
            return backlog;
        }

        public Boolean getKeepalive() {
            return keepalive;
        }

        public Integer getHeartBeat() {
            return heartBeat;
        }

        Integer port = 0;
        Integer backlog = 0;
        Boolean keepalive = true;
        Integer heartBeat = 0;
    }



    private AppConfig() {
        String propfileName = "Gateway.properties";

        PropertyFactory factory = new PropertyFactory(propfileName);
        nettyCfg = new NettyCfg();
        rocketMqCfg = new RocketMqCfg();
        serverCfg = new ServerCfg();
        queueCfg = new QueueCfg();
        redisCfg = new RedisCfg();

        nettyCfg.port = Integer.parseInt(factory.getValue("NETTY.TCP.PORT"));
        nettyCfg.backlog = Integer.parseInt(factory.getValue("NETTY.TCP.BACKLOG"));
        nettyCfg.keepalive = Boolean.parseBoolean(factory.getValue("NETTY.TCP.KEEPALIVE"));
        nettyCfg.heartBeat = Integer.parseInt(factory.getValue("NETTY.TCP.HEARTBEAT"));

        serverCfg.id = factory.getValue("SERVER.ID");
        serverCfg.name = factory.getValue("SERVER.NAME");

        queueCfg.trajectoryLength = Integer.parseInt(factory.getValue("SERVER.QUEUE.TRAJECTORY"));
        queueCfg.reportLength = Integer.parseInt(factory.getValue("SERVER.QUEUE.REPORT"));
        queueCfg.loginLength = Integer.parseInt(factory.getValue("SERVER.QUEUE.LOGIN"));
        queueCfg.diagnosisLength = Integer.parseInt(factory.getValue("SERVER.QUEUE.DIAGNOSIS"));
        queueCfg.cmdLength = Integer.parseInt(factory.getValue("SERVER.QUEUE.CMD"));

        rocketMqCfg.groupName = factory.getValue("ROCKETMQ.GROUPNAME");
        rocketMqCfg.nameServerAddr = factory.getValue("ROCKETMQ.NAMESERVERADDR");
        rocketMqCfg.diagnosisTopic = factory.getValue("ROCKETMQ.TOPIC.DIAGNOSIS");
        rocketMqCfg.loginTopic = factory.getValue("ROCKETMQ.TOPIC.LOGIN");
        rocketMqCfg.reportTopic = factory.getValue("ROCKETMQ.TOPIC.REPORT");
        rocketMqCfg.trajectoryTopic = factory.getValue("ROCKETMQ.TOPIC.TRAJECTORY");
        rocketMqCfg.cmdTopic = factory.getValue("ROCKETMQ.TOPIC.CMD");

        redisCfg.maxIdle = Integer.parseInt(factory.getValue("JEDIS.MAXIDLE"));
        redisCfg.maxTotal = Integer.parseInt(factory.getValue("JEDIS.MAXTOTAL"));
        redisCfg.maxWaitMillis = Integer.parseInt(factory.getValue("JEDIS.MAXWAITMILLIS"));
        redisCfg.host = factory.getValue("REDIS.HOST");
        redisCfg.port = Integer.parseInt(factory.getValue("REDIS.PORT"));
    }

    private static volatile AppConfig instance;

    public static AppConfig getInstance() {
        if (instance == null) {
            synchronized(AppConfig.class) {
                if (instance == null) {
                    instance = new AppConfig();
                }
            }
        }
        return instance;
    }


    public static void main(String[] args) {
        RocketMqCfg rocketMqCfg = AppConfig.getInstance().getRocketMqCfg();
        System.out.println(rocketMqCfg.getNameServerAddr());

    }


}
